

use "${data}/state-panel-final.dta", clear
cap drop treat post posttreat lnworkers
drop if year == "2007-08"

gen treat=(state=="Andhra Pradesh")
gen icode = real(industry)
gen manf = real(industry)>150 & real(industry)<370
drop if missing(icode)

egen uid = group(statecode icode)
xtset uid yr 
*tsfill, full 

*flagging the observations that were filled in by tsfill
gen miss_stateind = missing(state) 

foreach var of varlist statecode state icode industry treat {
	egen temp = mode(`var'), by(uid)
	replace `var' = temp if missing(`var')
	drop temp
}

foreach var of varlist numberoffactories factoriesinoperation {
	replace `var' = 0 if missing(`var')
}

gen post = (yr>=2003)
gen postXtreat = post*treat

gen lagN = L.numberoffactories
gen closed = numberoffactories -  factoriesinoperation
gen entry =  numberoffactories - L.numberoffactories + closed
gen frac_closed = closed/lagN
gen frac_entry = entry/lagN


la var frac_closed "Inoperational"
la var frac_entry "New Entrants"
la var postXtreat "Post X Treat"

global fe "statecode yr icode##yr"
global controls "i.statecode##c.yr"
global cl "cl(statecode)"



*************************
* MAIN TABLE - Table 6
*************************
est clear
local x = 0

foreach var of varlist frac_closed frac_entry {
*	local var = "futsup"
	
	local x = `x' + 1
	qui reghdfe `var' post treat postXtreat $controls , absorb($fe) $cl
	qui estadd local state_fe "Yes"
	qui estadd local year_fe "Yes"
	qui estadd local strend "Yes"
	qui estadd scalar rsq = e(r2)
	qui summ `var' if post == 0 & treat == 0 
	qui estadd scalar mean = `r(mean)'
	capture est sto reg_`x'

}


# delimit ;
esttab reg_?
using "$output/asi/Table6.tex", replace
keep(postXtreat)
mgroups("Fraction of firms", pattern(1 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Inactive" "New Entrants")
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
stats(mean rsq N, labels("Control, Pre Mean" "\midrule R2" "N")
fmt(2 2 0)) collabels(, none) label booktabs nonotes
;
#delimit cr	

